package com.market.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.mapper.MemberSourceInfoMapper;
import com.block.db.mapper.MerchantBaseInfoMapper;
import com.block.match.service.IMatchHandleService;
import com.market.member.service.IMerchantPushApiBInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商户申请信息 服务实现类
 * </p>
 *
 * @author
 * @since 2024-06-19
 */
@Service
public class MerchantApiBInfoServiceImpl implements IMerchantPushApiBInfoService {

    @Resource
    MerchantBaseInfoMapper merchantBaseInfoMapper;
    @Resource
    MemberSourceInfoMapper memberSourceInfoMapper;
    @Resource
    IMatchHandleService matchHandleService;


    @Override
    public ResponseVo pushApib(String uid) {

        MemberSourceInfo memberSourceInfo = memberSourceInfoMapper.getEntityByUidLock(Long.valueOf(uid));
        if (ObjectUtil.isEmpty(memberSourceInfo)) {
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "查询uid未空");
        }
        List<MerchantBaseInfo> merchantBaseInfos = merchantBaseInfoMapper.selectApib();
        if (merchantBaseInfos.size() == 0) {
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "暂无半流程产品");
        }

        ArrayList<JSONObject> merchantAbiBDataPushInfoVOArrayList = new ArrayList<>();

        for (MerchantBaseInfo merchantBaseInfo : merchantBaseInfos) {
            Integer infoId = merchantBaseInfo.getId();

            ResponseVo responseVo = matchHandleService.PushApBManual(String.valueOf(infoId), uid);

            if (!StrUtil.equals("0001",responseVo.getRspCode()) || ObjectUtil.isEmpty(responseVo.getData())){
                continue;
            }
            JSONObject merchantAbiBDataPushInfoVO = responseVo.getData().getJSONObject("merchantAbiBDataPushInfoVO");

            merchantAbiBDataPushInfoVOArrayList.add(merchantAbiBDataPushInfoVO);
        }

        return new ResponseVo(AppRspCodeConstant.SUCCESS, merchantAbiBDataPushInfoVOArrayList);
    }


}
